<?php

namespace App\Common;

use Hyperf\Redis\Redis;
use Hyperf\Redis\RedisFactory;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

trait RedisUtils
{
    protected $hKey = 'device';

    /**
     * @param $key
     * @param $data
     * @param $time
     * @return bool
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function set($key, $data, $time): bool
    {
        return ApplicationContext::getContainer()->get(Redis::class)->set($key, $data, $time);
    }

    /**
     * 删除缓存
     * @param $key
     * @return bool
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function del($key): bool
    {
        return ApplicationContext::getContainer()->get(Redis::class)->del($key);
    }

    /**
     * 查询缓存
     * @param $key
     * @return false|mixed|string
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function get($key)
    {
        return ApplicationContext::getContainer()->get(Redis::class)->get($key);
    }


    /**
     * @param $key
     * @param $data
     * @param $time
     * @return bool
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function hSet($key, $hashKey, $value): bool
    {
        return ApplicationContext::getContainer()->get(Redis::class)->hSet($key, $hashKey, $value);
    }

    /**
     * 删除缓存
     * @param $key
     * @return bool
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function hDel($key, $hashKey): bool
    {
        return ApplicationContext::getContainer()->get(Redis::class)->hDel($key, $hashKey);
    }

    /**
     * 查询缓存
     * @param $key
     * @return false|mixed|string
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function hGet($key, $hashKey)
    {
        return ApplicationContext::getContainer()->get(Redis::class)->hGet($key, $hashKey);
    }

    /**
     * 查询缓存
     * @param $key
     * @return false|mixed|string
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function hGetAll($key)
    {
        return ApplicationContext::getContainer()->get(Redis::class)->hGetAll($key);
    }

    /**
     * 查询缓存
     * @param $key
     * @return false|mixed|string
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function getChat($key)
    {
        return ApplicationContext::getContainer()->get(RedisFactory::class)->get('ChatToken')->get($key);
    }

    /**
     * 查询聊天所有key
     * @return false|mixed|string
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function getChatAllKey()
    {
        return ApplicationContext::getContainer()->get(RedisFactory::class)->get('ChatToken')->keys('chat_*');
    }

    /**
     * 设置缓存
     * @param $key
     * @param $data
     * @param $time
     * @return bool
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function setChat($key, $data, $time): bool
    {
        return ApplicationContext::getContainer()->get(RedisFactory::class)->get('ChatToken')->set($key, $data, $time);
    }

    /**
     * 删除登录
     * @param $key
     * @param $data
     * @param $time
     * @return bool
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function delChat($key): bool
    {
        return ApplicationContext::getContainer()->get(RedisFactory::class)->get('ChatToken')->del($key);
    }

    /**
     * 设置信息
     * @return bool
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \RedisException
     */
    public function zAdd($key, $time, $data): bool
    {
        return ApplicationContext::getContainer()->get(Redis::class)->zAdd($key, $time, $data);
    }

    /**
     * 获取位置
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function zRank(string $key, string $data)
    {
        return ApplicationContext::getContainer()->get(Redis::class)->zRank($key, $data);
    }

    /**
     * 删除
     * @param string $key
     * @param string $data
     * @return int
     */
    public function zRem($key, string $data)
    {
        return ApplicationContext::getContainer()->get(Redis::class)->zRem($key, $data);
    }

    /**
     * 获取聊天队列里的所有信息
     * @param string $key
     * @return array
     */
    public function zRange(string $key)
    {
        return ApplicationContext::getContainer()->get(Redis::class)->zRange($key, 0, -1);
    }

    /**
     * 获取判断是否存在
     * @param string $key
     * @return array
     */
    public function zScore(string $key,string $member)
    {
        return ApplicationContext::getContainer()->get(Redis::class)->zScore($key, $member);
    }

}